// Copyright (c) 2019-present, iQIYI, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

// Created by caikelun on 2019-09-25.
package com.sample.xcrash_lib;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityLifecycleCallbacks;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.ElementsCallback;
import ohos.global.configuration.Configuration;
import ohos.utils.PacMap;

import java.util.LinkedList;

class ActivityMonitor {

    private static final ActivityMonitor instance = new ActivityMonitor();

    private LinkedList<Ability> activities = null;
    private boolean isAppForeground = false;

    private static final int MAX_ACTIVITY_NUM = 100;

    private ActivityMonitor() {
    }

    static ActivityMonitor getInstance() {
        return instance;
    }

    void initialize(AbilityPackage abilityPackage) {

        activities = new LinkedList<Ability>();

        abilityPackage.registerCallbacks(new AbilityLifecycleCallbacks() {
            private int activityReferences = 0;
            private boolean isActivityChangingConfigurations = false;

            @Override
            public void onAbilityStart(Ability ability) {
                activities.addFirst(ability);
                if (activities.size() > MAX_ACTIVITY_NUM) {
                    activities.removeLast();
                }
            }

            @Override
            public void onAbilityActive(Ability ability) {

            }

            @Override
            public void onAbilityInactive(Ability ability) {

            }

            @Override
            public void onAbilityForeground(Ability ability) {

            }

            @Override
            public void onAbilityBackground(Ability ability) {

            }

            @Override
            public void onAbilityStop(Ability ability) {
                isActivityChangingConfigurations = (boolean) ability.onStoreDataWhenConfigChange();
                if (--activityReferences == 0 && !isActivityChangingConfigurations) {
                    isAppForeground = false;
                }
            }

            @Override
            public void onAbilitySaveState(PacMap pacMap) {

            }
        }, new ElementsCallback() {
            @Override
            public void onMemoryLevel(int i) {

            }

            @Override
            public void onConfigurationUpdated(Configuration configuration) {

            }
        });
    }

    void finishAllActivities() {
        if (activities != null) {
            for (Ability ability : activities) {
                ability.terminateAbility();
            }
            activities.clear();
        }
    }

    boolean isApplicationForeground() {
        return this.isAppForeground;
    }
}